package com.smart.community.feign.parking.fallback;

import com.smart.community.feign.common.BaseFeignFallbackFactory;
import com.smart.community.feign.parking.IParkingStatisticsFeign;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 停车统计Feign降级处理工厂
 * 
 * 继承BaseFeignFallbackFactory，使用统一的降级处理模板
 * 当停车服务不可用时，提供柔和的降级处理，避免大量错误日志
 * 
 * @author Wu.Liang
 * @version 2.0.0
 * @since 2025-01-30
 * @updated 2025-08-28 - 完善异常类型检查和日志记录
 */
@Slf4j
@Component
public class ParkingStatisticsFeignFallbackFactory extends BaseFeignFallbackFactory<IParkingStatisticsFeign> {

    @Override
    public IParkingStatisticsFeign create(Throwable cause) {
        // 使用基类的统一日志记录方法，区分异常类型
        if (isServiceUnavailable(cause)) {
            logServiceUnavailable(getServiceName(), cause);
        } else {
            log.warn("{}服务调用异常，执行降级处理: {}", getServiceName(), cause.getMessage());
        }
        
        return new IParkingStatisticsFeign() {
            @Override
            public Long countParkingSpacesByCommunityIds(List<Long> communityIds) {
                logMethodFallback("countParkingSpacesByCommunityIds", communityIds);
                return 0L; // 返回默认值，不影响业务
            }
            
            @Override
            public Long countParkingSpacesByPropertyCompanyIds(List<Long> propertyCompanyIds) {
                logMethodFallback("countParkingSpacesByPropertyCompanyIds", propertyCompanyIds);
                return 0L; // 返回默认值，不影响业务
            }
        };
    }
    
    @Override
    protected String getServiceName() {
        return "停车服务";
    }
}
